rm rootfs_ext4_ramdisk_switch_root.img
dd if=/dev/zero of=rootfs_ext4_ramdisk_switch_root.img bs=1M count=20
mkfs.ext4 rootfs_ext4_ramdisk_switch_root.img
mkdir -p tmpfs
mount -t ext4 rootfs_ext4_ramdisk_switch_root.img tmpfs/ -o loop
cp -af _install/* tmpfs/
touch tmpfs/hello.txt
echo test-test-switch_root > tmpfs/hello.txt
mv tmpfs/linuxrc tmpfs/init

cat > tmpfs/linuxrc <<EOF
#!/bin/sh
echo "Welcome to linux... $(date "+%Y%m%d%H%M%S")"
echo "current pid \$\$"
echo "hello world"

echo "mount -t sysfs sysfs /sys"
mount -t sysfs sysfs /sys

echo "mdev -s"
mdev -s

echo "mkdir /newroot"
mkdir /newroot

echo "mount -t ext4 /dev/vda /newroot"
mount -t ext4 /dev/vda /newroot

echo "sys"
umount /sys

#failed if using below method
#exec switch_root -c /dev/console /newroot /sbin/init

echo "cd /newroot"
cd /newroot

echo "mkdir -p oldroot"
mkdir -p oldroot

echo "pivot_root .  oldroot"
pivot_root .  oldroot

echo "exec chroot . /sbin/init <dev/console >dev/console 2>&1"
exec chroot . /sbin/init <dev/console >dev/console 2>&1

EOF

chmod +x tmpfs/linuxrc
cat tmpfs/linuxrc

umount tmpfs
rm -rf tmpfs
chmod 777 rootfs_ext4_ramdisk_switch_root.img
cp rootfs_ext4_ramdisk_switch_root.img rootfs_ext4_ramdisk_switch_root.img.bak
rm -f rootfs_ext4_ramdisk_switch_root.img.gz

gzip -v9 rootfs_ext4_ramdisk_switch_root.img
mkimage -A arm -O linux -T ramdisk -C gzip -n 'switch root ramdisk filesystem' -d rootfs_ext4_ramdisk_switch_root.img.gz urootfs_ext4_ramdisk_switch_root.img.gz
cp urootfs_ext4_ramdisk_switch_root.img.gz /home/coresat/tftproot/.
